import { Controller, Get, HttpStatus, Post, Query, Req } from '@nestjs/common';
import { ApiHeader, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Request } from 'express';
import { ValidAuthFailDto } from 'src/user/dto/middleware/auth.middleware.dto';
import {
  CreateDynamicItemReqDto,
  CreateDynamicItemResSuccessDto,
  CreateDynamicItemResFailDto,
} from './dto/create-dynamicitem.dto';
import {
  GetDynamicItemReqDto,
  GetDynamicItemResFailDto,
  GetDynamicItemResSuccessDto,
} from './dto/get-dynamicitem.dto';
import { DynamicService } from './dynamic.service';

@ApiTags('dynamic')
@ApiResponse({
  status: HttpStatus.FORBIDDEN,
  type: ValidAuthFailDto,
  description: '身份验证失败',
})
@Controller('dynamic')
export class DynamicController {
  constructor(private readonly dynamicService: DynamicService) {}

  @Post('item')
  @ApiHeader({
    name: 'token',
    description: '身份令牌',
  })
  @ApiResponse({
    type: CreateDynamicItemResSuccessDto,
    description: '创建动态成功',
    status: HttpStatus.CREATED,
  })
  @ApiResponse({
    type: CreateDynamicItemResFailDto,
    description: '创建动态失败',
    status: HttpStatus.ACCEPTED,
  })
  async createDynamicItem(
    @Req() request: Request,
    @Query() query: CreateDynamicItemReqDto,
  ) {
    const openid = request['user'].openid;
    const { article, background, awardId } = query;
    await this.dynamicService.addDynamic(openid, awardId, article, background);
    return {
      success: true,
    };
  }

  @Get('list')
  @ApiHeader({
    name: 'token',
    description: '身份令牌',
  })
  @ApiResponse({
    status: HttpStatus.FORBIDDEN,
    type: ValidAuthFailDto,
    description: '身份验证失败',
  })
  @ApiResponse({
    type: GetDynamicItemResSuccessDto,
    description: '获取动态成功',
    status: HttpStatus.OK,
  })
  @ApiResponse({
    type: GetDynamicItemResFailDto,
    description: '获取动态失败',
    status: HttpStatus.ACCEPTED,
  })
  async getList(@Req() request: Request, @Query() query: GetDynamicItemReqDto) {
    const openid = request['user'].openid;
    const { page, page_size } = query;

    const data = await this.dynamicService.getDynamics(
      Number(page),
      Number(page_size),
    );
    return {
      dynamics: data,
      success: true,
    };
  }
}
